package com.cr.front.user.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.cr.common.exception.ServiceException;
import com.cr.front.base.service.CaptchaService;
import com.cr.front.user.mapper.RoleMapper;
import com.cr.front.user.mapper.UserMapper;
import com.cr.front.user.pojo.dto.param.UserLoginParam;
import com.cr.front.user.pojo.entity.User;
import com.cr.front.user.pojo.vo.LoginResultVO;
import com.cr.front.user.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private CaptchaService captchaService;
    @Override
    public LoginResultVO login(UserLoginParam userLoginParam) {
        //检查验证码
        captchaService.checkCaptcha(userLoginParam.getCaptchaKey(),userLoginParam.getCaptcha());

        // 根据账号查询用户
        String account = userLoginParam.getAccount();
        User user = userMapper.selectSysUserByPhoneNum(account);
        // 判断账号是否不存在
        if (Objects.isNull(user)) {
            throw new ServiceException("账号或密码错误");
        }
        // 判断密码是否错误
        if (!BCrypt.checkpw(userLoginParam.getPassword(), user.getPassword())) {
            throw new ServiceException("账号或密码错误");
        }
        // 判断账号是否被停用
        if (Objects.nonNull(user.getStatus()) && user.getStatus() == 1) {
            throw new ServiceException("账号已经被停用");
        }

        // 查询用户的角色
        List<String> roles = roleMapper.selectRolesByUserId(user.getId());
        // 查询用户的权限
        List<String> perms = new ArrayList<>();
        perms.add("*");

        //Sa-Token 会话登录
        StpUtil.login(user.getId());


        //保存角色和权限到 Sa-Token 的session中,后期可以重复从里面拿roles和perms来使用
        StpUtil.getSession().set("roles",roles);
        StpUtil.getSession().set("perms",perms);
        StpUtil.getSession().set("nickname",user.getNickname());


        //构建登录结果VO对象
        LoginResultVO loginResultVO = new LoginResultVO();
        loginResultVO.setNickname(user.getNickname());
        loginResultVO.setAvatar(user.getAvatar());
        loginResultVO.setPerms(perms);
        loginResultVO.setToken(StpUtil.getTokenValue());


        // 返回登录结果VO对象
        return loginResultVO;



    }


//    @Autowired
//    private UserMapper userMapper;
//    @Override
//    public UserVO login(UserLoginParam userLoginParam) {
//        log.debug("用户登录，参数：{}",userLoginParam);
//        UserVO userVO = userMapper.selectByUsername(userLoginParam.getUsername());
//        if(userVO==null){
//            throw new ServiceException("用户名错误");
//        }
//        if(!userVO.getPassword().equals(userLoginParam.getPassword())){
//            throw new ServiceException("用户名密码错误");
//        }
//        return userVO;
//        return null;
//    }
//    @Override
//    public String reg(String userReginParam) {
//        log.debug("用户登录，参数：{}",userReginParam);
//        UserVO userVO = userMapper.selectByUsername(userReginParam);
//        if(userVO==null){
//            throw new ServiceException("用户名错误");
//        }
//        return userVO.getUsername();
//        return null;
//    }
}
